home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / Magic Oracle / BufferedDrawer.java next >
Encoding:
Java Source  |  2000-09-28  |  3.9 KB  |  129 lines  |  [TEXT/SNJ2]

  1. /**
  2.  *  Apple Worldwide Developer Technical Support
  3.  *
  4.  *  Sample demonstrating offscreen drawing.
  5.  *
  6.  *  by Levi Brown, Apple Developer Technical Support
  7.  *
  8.  *  File:   BufferedDrawer.java
  9.  *
  10.  *  Copyright ©1999 Apple Computer, Inc.
  11.  *  All rights reserved.
  12.  *
  13.  *    4/99    v. 1.0    Shipped as 'Magic Oracle AppleScript for Java' sample.
  14.  *
  15.  *  You may incorporate this sample code into your applications without
  16.  *  restriction, though the sample code has been provided "AS IS" and the
  17.  *  responsibility for its operation is 100% yours.  However, what you are
  18.  *  not permitted to do is to redistribute the source as "Apple Sample
  19.  *  Code" after having made changes. If you're going to re-distribute the
  20.  *  source, we require that you make it clear in the source that the code
  21.  *  was descended from Apple Sample Code, but that you've made changes.
  22. **/
  23.  
  24. import java.awt.Component;
  25. import java.awt.Image;
  26. import java.awt.Graphics;
  27. import java.awt.Dimension;
  28.  
  29. /**
  30.  * A component which does all it's drawing in an offscreen image
  31.  * the size of the component.
  32.  *
  33.  * @author Levi Brown
  34.  * @author Apple Worldwide Developer Technical Support
  35.  * @author Apple Computer Inc.
  36.  * @version 1.0 11/4/1998
  37.  */
  38. public class BufferedDrawer extends Component
  39. {
  40.     /**
  41.      * Creates a new BufferedDrawer.
  42.      */
  43.     public BufferedDrawer()
  44.     {
  45.         //Initialize our data members
  46.         workingGraphics     = null;
  47.         workingImage        = null;
  48.     }
  49.  
  50.     /**
  51.      * Handles drawing the desired content into the offscreen buffer.
  52.      * Override this to do your own drawing (be sure to call super.draw()).
  53.      * @see #paint
  54.      */
  55.     protected void draw()
  56.     {
  57.         //Make sure the offscreen image is ready to be drawn into.
  58.         ensureValidWorkingImage();
  59.  
  60.         //Do drawing here (use workingGraphics)
  61.         //Don't forget to erase the buffer if you don't draw over the entire area
  62.         //or else you will end up with tracers.
  63.         //To erase the entire area, uncomment out the following code:
  64.         /*
  65.         Dimension s = new Dimension(workingImage.getWidth(this), workingImage.getHeight(this));
  66.         workingGraphics.setColor(getBackground());
  67.         workingGraphics.fillRect(0, 0, s.width, s.height);
  68.         */
  69.     }
  70.  
  71.     /**
  72.      * Makes sure the offscreen image exists.
  73.      */
  74.     protected void ensureValidWorkingImage()
  75.     {
  76.         //If the size changed and we need to create a new pane image, then create one
  77.         Dimension size = getSize();
  78.         if ((size != null && size.width > 0 && size.height > 0) && (workingImage == null || size.width != workingImage.getWidth(this) || size.height != workingImage.getHeight(this)))
  79.         {
  80.             if (workingImage != null)
  81.             {
  82.                 workingImage.flush();
  83.                 workingImage = null;
  84.             }
  85.             workingImage = createImage(size.width, size.height);
  86.             if (workingGraphics != null)
  87.             {
  88.                 workingGraphics.dispose();
  89.                 workingGraphics = null;
  90.             }
  91.             workingGraphics = workingImage == null ? null : workingImage.getGraphics();
  92.         }
  93.     }
  94.  
  95.     /** 
  96.      * Updates the component. This method is called in
  97.      * response to a call to repaint. You can assume that
  98.      * the background is not cleared.
  99.      * Overriden here to prevent unwanted background erasing.
  100.      * @param g the specified Graphics window
  101.      * @see #paint
  102.      * @see java.awt.Component#repaint
  103.      */
  104.     public void update(Graphics g)
  105.     {
  106.         paint(g);
  107.     }
  108.  
  109.     /** 
  110.      * Paints the component.  This method is called when the contents
  111.      * of the component should be painted in response to the component
  112.      * first being shown or damage needing repair.  The clip rectangle
  113.      * in the Graphics parameter will be set to the area which needs
  114.      * to be painted.
  115.      * @param g the specified Graphics object
  116.      * @see #update
  117.      */
  118.     public void paint(Graphics g)
  119.     {
  120.         draw();
  121.         g.drawImage(workingImage, 0, 0, this);
  122.     }
  123.  
  124.     //The offscreen image buffer to render the progress bar into.
  125.     protected Image workingImage;
  126.     //The Graphics object associated with the offscreen image buffer.
  127.     protected Graphics workingGraphics;
  128. }
  129.